import re
import time

import pymysql.cursors
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# from selenium.webdriver import navigate


url = ''
pages = '/page/'

HOST_IP = 'localhost'  
PORT = '3306'
USER = 'root'
PASSWD = '123456'
DB_NAME = 'test_movie'

connection = pymysql.connect(
    host = HOST_IP,
    # port = PORT,
    user = USER,
    passwd = PASSWD,
    db = DB_NAME,
    charset = 'utf8mb4',
    use_unicode = True
)


# chrome_options = Options()
# chrome_options.add_argument("--headless")
# driver = webdriver.Chrome(chrome_options=chrome_options)
driver = webdriver.Chrome()
# driver.get(url)
# soup = BeautifulSoup(driver.page_source, features = 'lxml')
# divs = soup.find_all('div',{'class':'item masonry-brick'})
numbers = 6001
# for movies in divs:
#     movie = movies.find_all('a',{'class':'movie-box'})
#     movie = movie[0]
#     movie_address = str(url + movie['href'])

#     name = movies.find_all('img')
#     name = name[0]
#     movie_name = str(name['title'])
#     with connection.cursor() as cursor:
#         insert_sql = "INSERT INTO movie( number, movie_name,movie_address ) VALUES( %s, %s, %s )"
#         cursor.execute(insert_sql,(numbers, movie_name,movie_address))
#         connection.commit()
#         cursor.close()
#     print(numbers,'successful')
#     numbers += 1

for page in range(203,603):
    # driver.navigate().to(url+pages+str(page))
    try:
        driver.get(url+pages+str(page))
        time.sleep(3)
        soup = BeautifulSoup(driver.page_source,features='lxml')
        divs = soup.find_all('div',{'class':'item masonry-brick'})
        for movies in divs:
            movie = movies.find_all('a',{'class':'movie-box'})
            movie = movie[0]
            movie_address = str(url + movie['href'])

            name = movies.find_all('img')
            name = name[0]
            movie_name = str(name['title'])
            with connection.cursor() as cursor:
                insert_sql = "INSERT INTO movie( number, movie_name,movie_address ) VALUES( %s, %s, %s )"
                cursor.execute(insert_sql,(numbers, movie_name,movie_address))
                connection.commit()
                cursor.close()
            print(numbers,movie_name,'successful')
            numbers += 1
    finally:
        pass 

connection.close()
driver.close()